import provinces from '@/common/provinces'
import cities from '@/common/cities'
import areas from '@/common/areas'

export const centralAdmin = [{
      pCode: 110000,
      cCode: 110100
    }, {
      pCode: 120000,
      cCode: 120100
    }, {
      pCode: 500000,
      cCode: 500100
    }, {
      pCode: 310000,
      cCode: 310100
    }];

export default {
  confirm (msg) {
    return new Promise((resolve, reject) => {
        var result = window.confirm(msg)
        if (result) {
          resolve()
        } else {
          throw new Error('user cancel')
        }
    })
  },
  /**
   * 获取省级数据
   */
  getProvinces (code) {
    return Promise.resolve(code !== undefined ? provinces.select(item => ({ code: item.codeP, name: item.name })).find(c => c.code == code) : provinces.select(item => ({ code: item.codeP, name: item.name })));
  },
  /**
   * 获取城市信息 如果是直辖市，直接给县级行政区域
   */
  getCities (provinceCode, cityCode) {

    if (cityCode !== undefined) {
      return Promise.resolve(cities.filter(c => c.codeC == cityCode));
    }

    /**
     * 如果是直辖市的话
     */
    if (centralAdmin.some(c => c.pCode == provinceCode)) {
      var cCode = centralAdmin.find(c => c.pCode == provinceCode).cCode
      return Promise.resolve(areas.filter(c => c.codeC == cCode).select(item => ({ code: item.codeA, name: item.name, cityCode: item.codeC })))
    }

    /**
     * 不是直辖市
     */
    if (provinceCode) {
      return Promise.resolve(cities.select(c => ({name: c.name, provinceCode: c.codeP, cityCode: c.codeC})).filter(c => c.provinceCode == provinceCode));
    }

    return Promise.resolve(cities.select(c => ({name: c.name, provinceCode: c.codeP, cityCode: c.codeC})));
  },
  /**
   * 获取地区信息
   */
  getAreas () {

  }
}
